package ac.svn.accesscontrol.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ac.notification.event.INotificationListener;
import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.store.SVNTag;
import ac.svn.accesscontrol.ui.IUIConstants;
import ac.svn.accesscontrol.ui.UIComponentLibrary;
import ac.svn.accesscontrol.ui.window.TagEditorWindow;
import ac.svn.accesscontrol.utils.Utility;

public class TagController implements INotificationListener,
IResources,
IUIConstants,
ISVNIdentifier{

	protected static final String INTEL_C_TAG_CNTRLR = "TagController";
	private TagEditorWindow  editorWindow;
	private JMenuItem	miShowTagEditor;
	private JButton btnAddRemoveRight;
	private JButton btnEditUsers,btnAddUsers;
	private JButton btnEditGroups,btnAddGroups;
	private JButton btnEditLocations,btnAddLocations;

	public TagController()
	{
		editorWindow = new TagEditorWindow();
		UIComponentLibrary.addComponent(IUIConstants.C_TAG_EDITOR, editorWindow);

		miShowTagEditor = new JMenuItem("Tag Editor");
		miShowTagEditor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				editorWindow.showTagEditor();				
			}
		});

		JMenu toolsMenu = (JMenu) UIComponentLibrary.getComponent(IUIConstants.M_TOOL_MENU);
		if(null != toolsMenu)
		{
			toolsMenu.add(miShowTagEditor);
		}

		btnAddGroups = new JButton(Utility.getImageIcon(LOC_ICN_ADD_GROUPS));
		btnAddGroups.setToolTipText("Add Groups");
		btnEditGroups = new JButton(Utility.getImageIcon(LOC_ICN_EDIT_GROUPS));
		btnEditGroups.setToolTipText("Edit Groups");
		btnAddUsers    = new JButton(Utility.getImageIcon(LOC_ICN_ADD_USERS));
		btnAddUsers.setToolTipText("Add Users");		
		btnEditUsers    = new JButton(Utility.getImageIcon(LOC_ICN_EDIT_USERS));
		btnEditUsers.setToolTipText("Edit Users");
		btnAddLocations  = new JButton(Utility.getImageIcon(LOC_ICN_ADD_LOCATIONS));
		btnAddLocations.setToolTipText("Add Locations");
		btnEditLocations  = new JButton(Utility.getImageIcon(LOC_ICN_EDIT_LOCATIONS));
		btnEditLocations.setToolTipText("Edit Locations");

		btnAddRemoveRight = new JButton(Utility.getImageIcon(LOC_ICN_EDIT_ACCESS));
		btnAddRemoveRight.setToolTipText("Manager Access rights");

		JToolBar toolBar = (JToolBar) UIComponentLibrary.getComponent(IUIConstants.C_TOOL_BAR);
		toolBar.add(btnAddRemoveRight);
		toolBar.addSeparator();
		toolBar.add(btnAddUsers);
		toolBar.add(btnEditUsers);
		toolBar.addSeparator();
		toolBar.add(btnAddGroups);
		toolBar.add(btnEditGroups);
		toolBar.addSeparator();
		toolBar.add(btnAddLocations);
		toolBar.add(btnEditLocations);
		toolBar.addSeparator();


		NotificationRegistrar.registerListener(this);
		registerListener();
	}

	private void registerListener() {
		if(null != editorWindow)		
		{
			if(null != editorWindow.getTagsListView())
			{
				editorWindow.getTagsListView().addListSelectionListener(new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent lse) {
						//Check if event is not generated  on un-selection event.
						if(false == lse.getValueIsAdjusting())
						{
							//get Selected value
							SVNTag svnTag = (SVNTag) editorWindow.getTagsListView().getSelectedValue();
							//If any value is selected.
							if(null != svnTag)
							{
								if(null != editorWindow.getTextPane())
								{
									editorWindow.getTextPane().setText(svnTag.toString());
								}
								else
								{
									System.err.println("TextEditor in TagEditorWindow in not initialized.");
								}
							}
						}
					}
				});
			}
			else
			{
				System.err.println("List in TagEditor Window in not initialized.");
			}
		}

		btnAddRemoveRight.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});

		btnEditGroups.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				NotificationEvent event = new NotificationEvent(CMD_INTEL_SHOW);
				event.setPropertyValue(TYPE_INTEL_GROUP);
				event.addParameter(INTEL_OWNER_NAME, INTEL_C_TAG_CNTRLR);
				NotificationRegistrar.dispatchNotification(event);
			}
		});

		btnEditUsers.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				NotificationEvent event = new NotificationEvent(CMD_INTEL_SHOW);
				event.setPropertyValue(TYPE_INTEL_USER);
				event.addParameter(INTEL_OWNER_NAME, INTEL_C_TAG_CNTRLR);
				NotificationRegistrar.dispatchNotification(event);
			}
		});

	}

	@Override
	public Set<String> declaredSubscriptionSet() {
		Set<String> subscriptionSet = new HashSet<String>();
		subscriptionSet.add(INTEL_ITEM_SELECTION+INTEL_C_TAG_CNTRLR);
		return subscriptionSet;
	}

	@Override
	public void receiveEvent(NotificationEvent notificationEvent) {
		if(notificationEvent.getPropertyIdentifier().contains(INTEL_ITEM_SELECTION))
		{
			String intelSectionType = (String) notificationEvent.getParameter(INTEL_SELECTION_TYPE);
			if(intelSectionType.contains(TYPE_INTEL_GROUP))
			{
				System.err.println("Group Selection Made!");
			}else if(intelSectionType.contains(TYPE_INTEL_USER))
			{
				System.err.println("User Selection Made!");
			}
		}
	}

}
